package UI;
import model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * 服务器端渲染生成信息展示页面
 * 以表格的形式展示数据
 */
@WebServlet("/showlist")

public class ShowList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("信息列表");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");


        List<Student> stulist =(List<Student>) req.getAttribute("stulist");
        PrintWriter writer =resp.getWriter();
        writer.write("<!DOCTYPE html>");
        writer.write("<html>");
        writer.write("<head>");
        writer.write("</head>");
        writer.write("<body style='background:lightyellow'>");
        writer.write("<h1>学生信息展示页面</h1>");
        writer.write("<h3><a href='insert'>录入信息</a></h3>");
        writer.write("<table cellpadding ='3' cellspacing='0' border='1' align='center'>");
        writer.write("<tr><th>学号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>专业</th> <th>时间</th> <th>操作1</th> <th>操作2</th></tr>");
        for (int i= 0;i < stulist.size();i++){
            writer.write("<tr>");
            writer.write("<td>"+stulist.get(i).getId()+
                    "</td>");

            writer.write("<td>"+stulist.get(i).getName()+
                    "</td>");

            writer.write("<td>"+stulist.get(i).getSex()+
                    "</td>");

            writer.write("<td>"+stulist.get(i).getAge()+
                    "</td>");

            writer.write("<td>"+stulist.get(i).getMajor()+
                    "</td>");

            writer.write("<td>"+stulist.get(i).getTime()+
                    "</td>");

//            writer.write("<td><a onclick=\"if(confirm('确认删除吗？'))return true;return false;\" href=delOne?id="+stulist.get(i).getId()+">删除</a></td>");
            writer.write("<td onclick=\"if(confirm('确认删除吗？')){window.location.href='DelUser?id=" + stulist.get(i).getId() + "';}else{return false;}\">删除</td>");
            writer.write("<td onclick=\"if(confirm('确认更新吗？')){window.location.href='update?id=" + stulist.get(i).getId() + "';}else{return false;}\">更新</td>");

            writer.write("</tr>");
        }
        writer.write("</table>");
        writer.write("</body>");
        writer.write("</html>");
    }
}